package nciae.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import nciae.db.common.CommonResult;
import nciae.db.entity.Device;
import nciae.db.entity.User;
import nciae.db.mapper.DeviceMapper;
import nciae.db.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public CommonResult add(Device device) {
        CommonResult rs = new CommonResult();
        try {
            int k = deviceMapper.insert(device);

            if (k > 0) {
                rs.setCode(0);
                rs.setMsg("添加成功");
            } else {
                rs.setCode(-1);
                rs.setMsg("插入失败");
            }
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult edit(Device device) {
        CommonResult rs = new CommonResult();
        try {
            int k = deviceMapper.updateById(device);
            if (k > 0) {
                rs.setCode(0);
                rs.setMsg("修改成功");
            } else {
                rs.setCode(-1);
                rs.setMsg("修改失败");
            }
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult list(String name) {
        CommonResult rs = new CommonResult();
        try {
            QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
            if (name != null && !name.isEmpty()) {
                queryWrapper.eq("name", name);
            }

            List<Device> data = deviceMapper.selectList(queryWrapper);
            rs.setCode(0);
            rs.setMsg("查询成功");
            rs.setData(data);
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult listByPage(String name, Integer page, Integer size) {
        CommonResult rs = new CommonResult();
        try {
            Page<Device> pg = new Page<>(page,size);
            QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
            if (name != null && !name.isEmpty()) {
                queryWrapper.eq("name", name);
            }
            deviceMapper.selectPage(pg,queryWrapper);
            List<Device> data = pg.getRecords();
            long count = pg.getTotal();

            rs.setCode(0);
            rs.setMsg("查询成功");
            rs.setData(data);
            rs.setCount(count);
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public Device getDeviceById(Integer id) {
        Device device=deviceMapper.selectById(id);
        return device;
    }

    @Override
    public CommonResult del(Integer id) {
        CommonResult rs = new CommonResult();
        try{
            int k = deviceMapper.deleteById(id);
            if(k>0){
                rs.setCode(0);
                rs.setMsg("删除成功");
                return rs;
            }else{
                rs.setCode(-1);
                rs.setMsg("删除失败");
                return rs;
            }
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

}
